﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace Web_Blog.Controler
{
    public class Comment
    {
        //Load danh sách comment
        public static string LoadComment(int pID)
        {
            string spanreturn = "";

            ConnectDB.ConnectDB DB = new ConnectDB.ConnectDB();
            SqlConnection conn = DB.LayKetNoi();
            conn.Open();

            SqlCommand command3 = new SqlCommand("procLayComment", conn);
            command3.CommandType = CommandType.StoredProcedure;

            command3.Parameters.Add(new SqlParameter("@pID",pID));

            SqlDataReader reader3 = null;

            reader3 = command3.ExecuteReader();

            while (reader3.Read())
            {
                spanreturn += "<div class=\"noidungcomment\"><table style=\"width:580px\"> <tr> <td style=\"width:130px;text-align:center\" rowspan=\"2\"> <a href=\"TrangChu.aspx?u=";
                spanreturn += reader3["uID"].ToString();
                spanreturn += "\"><img style=\"width:75px;height:75px\" src=\"";
                spanreturn += Controler.Other.ParseAvatar(reader3["avatar"].ToString());
                spanreturn +="\" /></a><br />";
                spanreturn += "<a href=\"TrangChu.aspx?u=" + reader3["uID"].ToString() + "\">" + reader3["name"].ToString() + "</a>";
                spanreturn += "</td> <td style=\"max-width:450px> <p\">";
                spanreturn += reader3["contents"].ToString();
                spanreturn += "</p></td> </tr> <tr> <td> ";
                spanreturn += reader3["commentdate"].ToString();
                spanreturn += "</td> </tr> </table></div><br/>";
            }
            conn.Close();
            return spanreturn;
        }

        //Post một comment
        public static int PostComment(Object.PostCommentObject comment)
        {
            ConnectDB.ConnectDB DB = new ConnectDB.ConnectDB();
            SqlConnection conn = DB.LayKetNoi();
            conn.Open();

            SqlCommand command = new SqlCommand("procGuiComment", conn);
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@pID",comment.PID));
            command.Parameters.Add(new SqlParameter("@uID", comment.UID));
            command.Parameters.Add(new SqlParameter("@contents", comment.Contents));

            int retlval = command.ExecuteNonQuery();

            conn.Close();

            //Thêm thông báo
            Object.NotifyObject n = new Object.NotifyObject();
            n.Srcuid = comment.UID;
            n.Pid = comment.PID;
            n.Type = 2;
            Controler.Notification.AddNotification(n);

            return retlval;
        }
    }
}